home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CMDENABL.PAK / CMDENAB2.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  235 lines

  1. // ---------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (C) 1994, 1995 by Borland International, All Rights Reserved
  4. //
  5. //  Command-enabling sample application
  6. //  Second version implements command enabling for file commands.
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #include <owl/applicat.h>
  10. #include <owl/framewin.h>
  11. #include "cmdenabl.h"
  12.  
  13. //
  14. // class TCmdEnableApp
  15. // ~~~~~ ~~~~~~~~~~~~~
  16. // Application object initializes TApplication, overrides InitMainWindow.
  17. //
  18. class TCmdEnableApp : public TApplication {
  19.   public:
  20.     TCmdEnableApp() : TApplication() {}
  21.  
  22.   protected:
  23.     void InitMainWindow();
  24. };
  25.  
  26. //
  27. // class TCmdEnableWindow
  28. // ~~~~~ ~~~~~~~~~~~~~~~~
  29. // Window object initializes TWindow, adds four event handlers,
  30. // adds response table for four events.
  31. //
  32. class TCmdEnableWindow : public TWindow {
  33.   public:
  34.     TCmdEnableWindow(TWindow* parent = 0);
  35.  
  36.   protected:
  37.     // Event handlers
  38.     //
  39.     void CmFileNew();
  40.     void CmFileOpen();
  41.     void CmFileSave();
  42.     void CmFileSaveAs();
  43.     void CmToggleDirty();
  44.     void CmToggleNew();
  45.     void CmShowState();
  46.  
  47.     // Command-enabling functions
  48.     //
  49.     void CeFileNew(TCommandEnabler&);
  50.     void CeFileOpen(TCommandEnabler&);
  51.     void CeFileSave(TCommandEnabler&);
  52.  
  53.     bool IsDirty;
  54.     bool IsNewFile;
  55.  
  56.   DECLARE_RESPONSE_TABLE(TCmdEnableWindow);
  57. };
  58.  
  59.  
  60. //
  61. //
  62. //
  63. DEFINE_RESPONSE_TABLE1(TCmdEnableWindow, TWindow)
  64.   EV_COMMAND(CM_FILENEW, CmFileNew),
  65.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  66.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  67.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  68.   EV_COMMAND(CM_TOGGLEDIRTY, CmToggleDirty),
  69.   EV_COMMAND(CM_TOGGLENEW, CmToggleNew),
  70.   EV_COMMAND(CM_SHOWSTATE, CmShowState),
  71.   EV_COMMAND_ENABLE(CM_FILENEW, CeFileNew),
  72.   EV_COMMAND_ENABLE(CM_FILEOPEN, CeFileOpen),
  73.   EV_COMMAND_ENABLE(CM_FILESAVE, CeFileSave),
  74. END_RESPONSE_TABLE;
  75.  
  76. //
  77. // TCmdEnableApp::InitMainWindow
  78. //
  79. void
  80. TCmdEnableApp::InitMainWindow()
  81. {
  82.   // Construct the frame window
  83.   //
  84.   TFrameWindow* frame = new TFrameWindow(0, "Command-enabling sample application",
  85.     new TCmdEnableWindow, true);
  86.  
  87.   // Set the main window and its menu
  88.   //
  89.   SetMainWindow(frame);
  90.   GetMainWindow()->AssignMenu(MN_COMMANDS);
  91. }
  92.  
  93.  
  94. //
  95. //
  96. //
  97. TCmdEnableWindow::TCmdEnableWindow(TWindow* parent)
  98. :
  99.   TWindow(parent),
  100.   IsNewFile(true),
  101.   IsDirty(false)
  102. {
  103. }
  104.  
  105.  
  106. //
  107. //
  108. //
  109. void
  110. TCmdEnableWindow::CmFileNew()
  111. {
  112.   IsDirty   = false;
  113.   IsNewFile = true;
  114.   MessageBox("Opened a new file.\nIsDirty=false\nIsNewFile=true",
  115.     "File action taken");
  116. }
  117.  
  118.  
  119. //
  120. //
  121. //
  122. void
  123. TCmdEnableWindow::CmFileOpen()
  124. {
  125.   IsDirty   = false;
  126.   IsNewFile = false;
  127.   MessageBox("Opened an existing file.\nIsDirty=false\nIsNewFile=false",
  128.     "File action taken");
  129. }
  130.  
  131.  
  132. //
  133. //
  134. //
  135. void
  136. TCmdEnableWindow::CmFileSave()
  137. {
  138.   IsDirty   = false;
  139.   IsNewFile = false;
  140.   MessageBox("Saved an existing file.\nIsDirty=false\nIsNewFile=false",
  141.     "File action taken");
  142. }
  143.  
  144.  
  145. //
  146. //
  147. //
  148. void
  149. TCmdEnableWindow::CmFileSaveAs()
  150. {
  151.   IsDirty   = false;
  152.   IsNewFile = false;
  153.   MessageBox("Saved a file under a new name.\nIsDirty=false\nIsNewFile=false",
  154.     "File action taken");
  155. }
  156.  
  157.  
  158. //
  159. //
  160. //
  161. void
  162. TCmdEnableWindow::CmToggleDirty()
  163. {
  164.   IsDirty = !IsDirty;
  165. }
  166.  
  167.  
  168. //
  169. //
  170. //
  171. void
  172. TCmdEnableWindow::CmToggleNew()
  173. {
  174.   IsNewFile = !IsNewFile;
  175. }
  176.  
  177.  
  178. //
  179. //
  180. //
  181. void
  182. TCmdEnableWindow::CmShowState()
  183. {
  184.   string str(IsDirty ? "IsDirty = true\n" : "IsDirty = false\n");
  185.   str += (IsNewFile ? "IsNewFile = true" : "IsNewFile = false");
  186.   MessageBox(str.c_str(), "Current state");
  187. }
  188.  
  189.  
  190. //
  191. //
  192. //
  193. void
  194. TCmdEnableWindow::CeFileNew(TCommandEnabler& ce)
  195. {
  196.   // Enable CmFileNew if not dirty
  197.   //
  198.   ce.Enable(!IsDirty);
  199. }
  200.  
  201.  
  202. //
  203. //
  204. //
  205. void
  206. TCmdEnableWindow::CeFileOpen(TCommandEnabler& ce)
  207. {
  208.   // Enable CmFileSave if not new file and is dirty.
  209.   //
  210.   ce.Enable(!IsNewFile && IsDirty);
  211. }
  212.  
  213.  
  214. //
  215. //
  216. //
  217. void
  218. TCmdEnableWindow::CeFileSave(TCommandEnabler& ce)
  219. {
  220.   // Enable CmFileSave if not new file
  221.   //
  222.   ce.Enable(!IsNewFile);
  223. }
  224.  
  225.  
  226. //
  227. // Put the OwlMain here just to get it out of the way
  228. //
  229. int
  230. OwlMain(int /*argc*/, char* /*argv*/ [])
  231. {
  232.   return TCmdEnableApp().Run();
  233. }
  234.  
  235.